package models;

import java.util.Observable;

public class Game extends Observable
{
    private Board board;
    private Logic logic;
    private GameTimer timer;
    private TopTen score;
    private int confg;

    public Game()
    {
        board = Board.getInstance();
        logic = Logic.getInstance();
        timer = new GameTimer(this);
        score = new TopTen();
    }

    public void setConfig(int conf)
    {
        confg = conf;
    }

    public void newGame()
    {
        board.reset();
        logic.generate(confg);
        timer.start();
        setChanged();
        notifyObservers("Matrix");
    }

    public void stop()
    {
        board.reset();
        timer.stop();
        setChanged();
        notifyObservers("Matrix");
    }

    public void pause()
    {
        timer.pause();
    }

    public void resume()
    {
        timer.resume();
    }

    public void setNumberOnBoard(int row, int column, int value)
    {
        board.setNumber(row, column, value);
        setChanged();
        notifyObservers("Number");
    }

    public boolean verify()
    {
        return logic.verify();
    }

    public boolean isTopScore()
    {
        return score.isTop(score());
    }
    
    private int score() 
    {
    	return (100000/timer.getTimeInSeconds());
    }
    
    public String[] getTopTen() {
    	return score.getTopTenScores();
    }

    public void saveScore(String name)
    {
        score.refreshScores(score(), name);
    }

    public GameTimer getTimer()
    {
        return timer;
    }

    public void meRindo()
    {
        board.setMatrix(logic.getSolutionMatrix());
        timer.pause();
        setChanged();
        notifyObservers("Matrix");
    }

    public void vaciar()
    {
        board.setMatrix(logic.getInitialMatrix());
        setChanged();
        notifyObservers("Matrix");
    }

    public Logic getLogic()
    {
        return logic;
    }
}
